package com.cainli.image;

import com.cainli.image.URLDrawable.URLDrawableListener;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;


public class URLImageView extends ImageView implements URLDrawableListener
{
    private URLDrawableDownListener listener;

    public URLImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        CharSequence description = getContentDescription();
        if(!TextUtils.isEmpty(description))
        {
            try
            {
                setImageURL(description.toString());
            }
            catch (IllegalArgumentException e)
            {
                Log.w("URLImageView", "Illegal url: " + description);
            }
        }
    }

    public URLImageView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public URLImageView(Context context)
    {
        this(context, null);
    }
    
    public void setURLDrawableDownListener(URLDrawableDownListener listener){
        this.listener = listener;
    }

    @Override
    public void onLoadSuccessed(URLDrawable drawable)
    {
        if (getDrawable() == drawable)
        {
            super.setImageDrawable(null);
            super.setImageDrawable(drawable);
        }
        else if (getBackground() == drawable)
        {
            super.setImageDrawable(null);
            super.setImageDrawable(drawable);
        }
        if (listener != null)
        {
            listener.onLoadSuccessed(this, drawable);
        }
        // 如果是渐显方式, 则使用animation, 性能好一点
        if (drawable.mFadeInImage)
        {
            drawable.mFadeInImage = false;
            Animation anim = new AlphaAnimation(0, 1);
            anim.setDuration(600);
            startAnimation(anim);
        }
    }

    @Override
    public void onLoadFialed(URLDrawable drawable, Throwable cause)
    {
        if (getDrawable() == drawable)
        {
            super.setImageDrawable(null);
            super.setImageDrawable(drawable);
        }
        else if (getBackground() == drawable)
        {
            super.setImageDrawable(null);
            super.setImageDrawable(drawable);
        }
        if (listener != null)
        {
            listener.onLoadFialed(this, drawable, null);
        }
    }

    @Override
    public void onLoadCanceled(URLDrawable drawable)
    {
        if (listener != null)
        {
            listener.onLoadCancelled(this, drawable);
        }
    }
    
    /**
     * 设置前景URL
     * 
     * @param url
     */
    public void setImageURL(String url) {
        if(url == null)
        {
            throw new IllegalArgumentException("url can't be null");
        }
        if (getDrawable() instanceof URLDrawable)
        {
            URLDrawable drawable = (URLDrawable) getDrawable();
            if(drawable.getURL().toString().equals(url))
            {
                return;
            }
        }
        setImageDrawable(URLDrawable.getDrawable(url));
    }
    
    @Override
    public void setImageDrawable(Drawable drawable) {
        Drawable current = this.getDrawable();
        if(current == drawable)
        {
            return;
        }
        // 清掉旧的
        if (current instanceof URLDrawable)
        {
            ((URLDrawable) current).setURLDrawableListener(null);
        }
        
        if (drawable instanceof URLDrawable)
        {
            ((URLDrawable) drawable).setURLDrawableListener(this);
        }
        super.setImageDrawable(drawable);
    }
    
    /**
     * 设置背景URL
     * 
     * @param url
     */
    public void setBackgroundURL(String url) {
        
        if(url == null)
        {
            throw new IllegalArgumentException("url can't be null");
        }
        if (getBackground() instanceof URLDrawable)
        {
            URLDrawable drawable = (URLDrawable) getBackground();
            if(drawable.getURL().toString().equals(url))
            {
                return;
            }
        }
        setBackgroundDrawable(URLDrawable.getDrawable(url));
    }
    
    @Override
    public void setBackgroundDrawable(Drawable drawable)
    {
        Drawable current = this.getBackground();
        if(current == drawable)
        {
            return;
        }
        
        if(current instanceof URLDrawable)
        {
            ((URLDrawable) current).setURLDrawableListener(null);
        }
        
        if (drawable instanceof URLDrawable)
        {
            ((URLDrawable) drawable).setURLDrawableListener(this);
        }
        super.setBackgroundDrawable(drawable);
    }

}